﻿using CSharpDay13Nov10.Data.Model;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Migrations; //For AddOrUpdate

namespace CSharpDay13Nov10.Data
{
    public static class Seeder
    {
        public static void Seed(ApplicationDbContext db, bool roles = false, bool users = false, bool tasks = false)
        {
            if (roles) SeedRoles(db);
            if (users) SeedUsers(db);
            if (tasks) SeedTasks(db);
        }
        private static void SeedRoles(ApplicationDbContext db)
        {
            var manager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));

            if (!db.Roles.Any(x => x.Name == "User"))
            {
                manager.Create(new IdentityRole() { Name = "User" });
            }
            if (!db.Roles.Any(x => x.Name == "Admin"))
            {
                manager.Create(new IdentityRole() { Name = "Admin" });
            }
        }
        private static void SeedUsers(ApplicationDbContext db)
        {
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));

            if (!db.Users.Any(x => x.UserName == "Admin"))
            {
                ApplicationUser user = new ApplicationUser()
                {
                    UserName = "Admin",
                    Email = "admin@test.com"
                };
                manager.Create(user, "123123");
                manager.AddToRole(user.Id, "Admin");
            }
            if (!db.Users.Any(x => x.UserName == "user1"))
            {
                ApplicationUser user = new ApplicationUser()
                {
                    UserName = "user1",
                    Email = "user1@test.com"
                };
                manager.Create(user, "123123");
                manager.AddToRole(user.Id, "User");
            }
            if (!db.Users.Any(x => x.UserName == "user2"))
            {
                ApplicationUser user = new ApplicationUser()
                {
                    UserName = "user2",
                    Email = "user2@test.com"
                };
                manager.Create(user, "123123");
                manager.AddToRole(user.Id, "User");
            }
            if (!db.Users.Any(x => x.UserName == "user3"))
            {
                ApplicationUser user = new ApplicationUser()
                {
                    UserName = "user3",
                    Email = "user3@test.com"
                };
                manager.Create(user, "123123");
                manager.AddToRole(user.Id, "User");
            }
        }
        private static void SeedTasks(ApplicationDbContext db)
        {
            string AdminId = db.Users.Where(x => x.UserName == "Admin").Select(x => x.Id).FirstOrDefault();
            string User1Id = db.Users.Where(x => x.UserName == "user1").Select(x => x.Id).FirstOrDefault();
            string User2Id = db.Users.Where(x => x.UserName == "user2").Select(x => x.Id).FirstOrDefault();
            string User3Id = db.Users.Where(x => x.UserName == "user3").Select(x => x.Id).FirstOrDefault();

            db.ToDos.AddOrUpdate(x => x.Task,
                new ToDo() { Task = "Clean The Room 1", UserId = AdminId, IsDeleted = false },
                new ToDo() { Task = "Clean The Room 2", UserId = AdminId, IsDeleted = false },
                new ToDo() { Task = "Clean The Room 3", UserId = User1Id, IsDeleted = false },
                new ToDo() { Task = "Clean The Room 4", UserId = User1Id, IsDeleted = true },
                new ToDo() { Task = "Clean The Room 5", UserId = User1Id, IsDeleted = false },
                new ToDo() { Task = "Clean The Room 6", UserId = User2Id, IsDeleted = false },
                new ToDo() { Task = "Clean The Room 7", UserId = User2Id, IsDeleted = true },
                new ToDo() { Task = "Clean The Room 8", UserId = User3Id, IsDeleted = false },
                new ToDo() { Task = "Clean The Room 9", UserId = User3Id, IsDeleted = false }
                );
            db.SaveChanges();
        }
    }
}
